<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择小区：" prop="plotId">
        <el-select v-model="queryParams.plotId" placeholder="请选择小区">
          <el-option v-for="(item, index) in xiaoquData" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称：" prop="classifyName">
        <el-input v-model="queryParams.classifyName" placeholder="请输入分类名称" clearable />
      </el-form-item>
      <el-form-item label="创建时间：" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="yyyy-MM-dd" type="daterange" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <!-- <el-form-item label="小区名称" prop="plotName">
        <el-input v-model="queryParams.plotName" placeholder="请输入小区名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类名称" prop="classifyName">
        <el-input v-model="queryParams.classifyName" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['householdClassify:householdClassify:add']">新增分类</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['householdClassify:householdClassify:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="pilinagHandleCilcik"
          v-hasPermi="['householdClassify:householdClassify:remove']">批量删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['householdClassify:householdClassify:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="householdClassifyList" @selection-change="handleSelectionChange">
      <el-table-column v-if="selectFlag" show-overflow-tooltip type="selection" width="55" align="center" />
      <!-- <el-table-column show-overflow-tooltip label="住户分类id/序号" align="center" prop="id" /> -->
      <!-- <el-table-column show-overflow-tooltip label="小区id" align="center" prop="plotId" /> -->
      <el-table-column show-overflow-tooltip label="小区名称" align="center" prop="plotName" />
      <el-table-column show-overflow-tooltip label="分类名称" align="center" prop="classifyName" />
      <el-table-column show-overflow-tooltip label="创建人" align="center" prop="createName" />
      <el-table-column show-overflow-tooltip label="创建时间" align="center" prop="createTime" />
      <el-table-column show-overflow-tooltip label="描述" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['householdClassify:householdClassify:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['householdClassify:householdClassify:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改住户分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="80px">
        <div style="display: flex;flex-wrap: wrap">
          <el-form-item label="选择小区：" prop="plotId" style="margin-right: 10%;margin-left: 20px;">
            <el-select :disabled="form.id" v-model="form.plotId" placeholder="请选择小区">
              <el-option v-for="(item, index) in xiaoquDataAdd" :key="index" :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分类名称：" prop="classifyName" style="margin-left: 20px;">
            <el-input v-model="form.classifyName" placeholder="请输入分类名称" />
          </el-form-item>
        </div>
        <el-form-item label="描述：" prop="remark" style="margin-right: 50px;margin-left: 20px;">
          <el-input type="textarea" show-word-limit v-model="form.remark" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div v-if="form.id" class="tableBox">
        <h4 class="name">分类住户列表</h4>
        <el-table v-loading="HouseholdData.loading" :data="HouseholdData.list">
          <el-table-column show-overflow-tooltip label="小区名称" align="center" prop="plotName" />
          <el-table-column show-overflow-tooltip label="所属楼栋" align="center" prop="buildName" />
          <el-table-column show-overflow-tooltip label="所属单元" align="center" prop="unitName" />
          <el-table-column show-overflow-tooltip label="楼层" align="center" prop="floor" />
          <el-table-column show-overflow-tooltip label="户号" align="center" prop="houseNumber" />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)"
                v-hasPermi="['householdClassify:householdClassify:remove']">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="myPagination">
          <pagination v-show="HouseholdData.total > 0" :total="HouseholdData.total" :page.sync="HouseholdData.pageNum"
            :limit.sync="HouseholdData.pageSize" @pagination="getHouseholdList" />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHouseholdClassify, getHouseholdClassify, delHouseholdClassify,
  addHouseholdClassify, updateHouseholdClassify
} from "@/api/householdClassify/householdClassify"
import { listHousehold,updateHousehold } from "@/api/house/household"
// import { householdClassifyList } from "@/api/mock"
// import data5 from "@/utils/data5.json";
import { listCPlotSelect } from "@/api/system/utils"


export default {
  name: "HouseholdClassify",
  data() {
    return {
      xiaoquData: [],
      xiaoquDataAdd: [],
      fenzuData: [],
      selectFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 住户分类表格数据
      householdClassifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plotId: null,
        plotName: null,
        classifyName: null,
        createTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        plotId: [
          { required: true, message: "小区id不能为空", trigger: "blur" }
        ],
        classifyName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      HouseholdData: {
        loading: false,
        typeId: null,
        total: 0,
        pageNum: 1,
        pageSize: 10,
        list: [],
      }
    };
  },
  created() {
    listCPlotSelect({}).then(res => {
      this.xiaoquData = res.rows
      this.xiaoquDataAdd = res.rows
      this.queryParams.plotId = res.rows[0].value
    }).then(res=>{
      this.getList();
    });
  },
  methods: {
    /** 查询住户分类列表 */
    getList() {
      this.loading = true;
      listHouseholdClassify(this.queryParams).then(response => {
        // householdClassifyList(this.queryParams).then(response => {
        console.log(response.rows);
        this.householdClassifyList = response.rows;
        this.total = response.total;
        this.loading = false;
      },(err) => {
        console.log("这是错误",err);
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plotId: null,
        plotName: null,
        classifyName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加住户分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      this.HouseholdData.typeId = id
      getHouseholdClassify(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改住户分类";
      })
      this.getHouseholdList()
    },
    // 获取分类住户列表
    getHouseholdList() {
      let id = this.HouseholdData.typeId
      let pageNum = this.HouseholdData.pageNum
      this.HouseholdData.loading = true
      listHousehold({ type: id, pageNum }).then(res => {
        this.HouseholdData.loading = false
        this.HouseholdData.total = res.total
        this.HouseholdData.list = res.rows
      }).catch(err => {
        this.HouseholdData.loading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.plotName = this.xiaoquDataAdd.filter(item => item.value == this.form.plotId)[0].label;
          console.log(this.xiaoquDataAdd.filter(item => item.value == this.form.plotId))
          console.log(this.form)
          if (this.form.id != null) {
            updateHouseholdClassify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouseholdClassify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 批量删除
    pilinagHandleCilcik() {
      const ids = this.ids;
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        this.$modal.confirm('是否删除所选数据？').then(function () {
          return delHouseholdClassify(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$modal.confirm('是否确认删除住户分类数据？').then(function () {
        return delHouseholdClassify(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    //移除分类上的住户
    handleRemove(row) {
      this.$modal.confirm(`是否确认移除户号为${row.houseNumber}的住户？`).then(() => {
        row.type = ''
        updateHousehold(row).then(res => {
          this.$modal.msgSuccess("移除成功")
          this.getHouseholdList()
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('householdClassify/householdClassify/exportHouseholdClassify', {
        ...this.queryParams
      }, `householdClassify_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .tableBox {

  .name {
    color: #fff;
    font-size: 18px;
  }

  // .el-table {
  //   background-color: #374056;
  // }

  .pagination-container .el-pagination {
    position: relative;
  }

  .el-table .el-table__body-wrapper {
    height: auto !important;
    min-height: 200px;
  }

  .myPagination {
    display: flex;
    justify-content: center;

    .el-dialog .el-select>.el-input {
      width: 100px;
    }

    .el-pagination .el-select .el-input .el-input__inner {
      height: 28px;
    }

    .el-pagination__editor.el-input .el-input__inner {
      width: auto;
      border-radius: 4px;
    }
  }


}
</style>
